package org.goyo.main.services.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.system.ISysDepGroupDao;
import org.goyo.main.domain.dao.system.ISysResourceDao;
import org.goyo.main.domain.dao.system.ISysRoleResourceDao;
import org.goyo.main.domain.dao.system.ISysUserDao;
import org.goyo.main.domain.dao.system.ISysUserDepGroupDao;
import org.goyo.main.domain.dao.system.ISystemDao;
import org.goyo.main.domain.entity.system.SysDepGroup;
import org.goyo.main.domain.entity.system.SysLog;
import org.goyo.main.domain.entity.system.SysResource;
import org.goyo.main.domain.entity.system.SysRoleResource;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.domain.entity.system.SysUserDepGroup;
import org.goyo.main.services.system.ISystemService;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class SystemServiceImpl implements ISystemService {
	private Cache _cache;
	@Resource
	private ISystemDao systemDao;
	@Resource
	private CacheManager cacheManager;
	@Resource
	private ISysResourceDao sysResourceDao;
	@Resource
	private ISysRoleResourceDao sysRoleResourceDao;
	@Resource
	private ISysUserDepGroupDao sysUserDepGroupDao;
	@Resource
	private ISysDepGroupDao sysDepGroupDao;
	@Resource
	private ISysUserDao sysUserDao;
	
	@Override
	public List<SysResource> findSysResourceByUserName(String userName) {
		return null;
	}
	
	/**
	 * 初始化
	 */
	@PostConstruct
	public void init() {
		_cache = cacheManager.getCache(Constants.SYSTEM);
	}
	
	@Override
	public Map<String,List<SysRoleResource>> getAllAuthority() {
		if(_cache.get(Constants.USERAUTHORITY) == null){
			refreshAuthority();
		}
		Map<String, List<SysRoleResource>> map = (Map<String, List<SysRoleResource>>) _cache.get(Constants.USERAUTHORITY).get();
		return map;
	}

	@Override
	public void refreshAuthority() {
		Map<String,  List<SysRoleResource>> map = new HashMap<String,  List<SysRoleResource>>();
		//查询全部资源
			List<SysResource> resourcesList = sysResourceDao.getAll(SysResource.class);
			//根据资源查询所需要的角色
			for(SysResource resources : resourcesList){
				List<SysRoleResource> roleResources = sysRoleResourceDao.findByResourceId(resources.getUuid());
				map.put(resources.getUrl(),roleResources);
			}
		_cache.put(Constants.USERAUTHORITY, map);
	}

  @Override
  @Transactional(propagation=Propagation.REQUIRES_NEW) 
  public void saveLog(SysLog slog) throws DBException, ServiceException {
    sysResourceDao.saveOrUpdate(slog);
  }

  @Override
  public Pager findByCondition(String where, Pager pager) throws DBException, ServiceException {
    return systemDao.findByCondition(where,pager);
  }
  
  
  @Override
	public Map<String,List<SysDepGroup>> getAllDepGroup() {
		if(_cache.get(Constants.USERDEPGROUP) == null){
			refreshDepGroup();
		}
		Map<String, List<SysDepGroup>> map = (Map<String, List<SysDepGroup>>) _cache.get(Constants.USERDEPGROUP).get();
		return map;
	}

	@Override
	public void refreshDepGroup() {
		Map<String,  List<SysDepGroup>> map = new HashMap<String,  List<SysDepGroup>>();
		//查询全部用户
		List<SysUser> userList = sysUserDao.getAll(SysUser.class);
		for(SysUser user : userList){
			List<SysUserDepGroup> userGroupList =  sysUserDepGroupDao.findByUserId(user.getUuid());
			List<SysDepGroup> groups = new ArrayList<SysDepGroup>();
			for(SysUserDepGroup userGroup : userGroupList){
				SysDepGroup group = sysDepGroupDao.findByUuid(SysDepGroup.class, userGroup.getDepGroupId());
				groups.add(group);
			}
			map.put(user.getUuid(), groups);
		}
		_cache.put(Constants.USERDEPGROUP, map);
	}
}
